home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / customcontroldemo / gradientbackgroundex.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  6.0 KB  |  137 lines

  1. Imports System.ComponentModel
  2. Imports System.Drawing.Design
  3. Imports System.Drawing.Drawing2D
  4. Imports System.Windows.Forms.Design
  5.  
  6. ' An improved version of the GradientBackground control
  7. ' with a new RotateAngle property
  8.  
  9. Public Class GradientBackgroundEx
  10.     Inherits GradientBackground
  11.  
  12.     Dim m_RotateAngle As Single
  13.  
  14.     <Description("The rotation angle for the gradient brush"), DefaultValue(0), _
  15.      Editor(GetType(RotateAngleEditor), GetType(UITypeEditor))> _
  16.     Property RotateAngle() As Single
  17.         Get
  18.             Return m_RotateAngle
  19.         End Get
  20.         Set(ByVal Value As Single)
  21.             m_RotateAngle = Value
  22.             Me.Invalidate()
  23.         End Set
  24.     End Property
  25.  
  26.     ' redefine the OnPaint event to account for the new property.
  27.  
  28.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  29.         ' Create a gradient brush as large as the client area, with specified
  30.         ' start/end color and gradient mode
  31.         Dim br As New Drawing2D.LinearGradientBrush(Me.ClientRectangle, Me.StartColor, Me.EndColor, Me.GradientMode)
  32.         ' apply the rotation angle
  33.         br.RotateTransform(Me.RotateAngle)
  34.         ' paint the background.
  35.         e.Graphics.FillRectangle(br, Me.ClientRectangle)
  36.         ' orderly destroy the brush.
  37.         br.Dispose()
  38.     End Sub
  39.  
  40.     '------------------------------------------------------------------------
  41.     ' The Editor for the RotateAngle property
  42.     '------------------------------------------------------------------------
  43.  
  44.     Class RotateAngleEditor
  45.         Inherits UITypeEditor
  46.  
  47.         ' Override the GetEditStyle to tell that this editor supports DropDown style.
  48.         ' The context object passed to this function provides information about the
  49.         ' context, and context.Instance is the control being editor.
  50.  
  51.         Overloads Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
  52.             If Not (context Is Nothing) AndAlso Not (context.Instance Is Nothing) Then
  53.                 Return UITypeEditorEditStyle.DropDown
  54.             End If
  55.             Return MyBase.GetEditStyle(context)
  56.         End Function
  57.  
  58.         ' This is the TrackBar control that will be displayed in the editor
  59.         Dim WithEvents tb As TrackBar
  60.         ' this the the object that represent the service in the editor that
  61.         ' creates the dropdown portion or shows a dialog.
  62.         Dim wfes As IWindowsFormsEditorService
  63.  
  64.         ' Override the EditValue function.
  65.         ' The Provider argument lets you query for an IWindowsFormsEditorService object, which in
  66.         ' turn lets you access the Properties window, and do things such as opening/closing the
  67.         ' dropdown area,or display a modal form
  68.         ' The Value argument is the current value for the property.
  69.         ' The return value must be the new value of the property.
  70.  
  71.         Overloads Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
  72.             ' Exit without changing the value if no context, instance, or provider is provided
  73.             If (context Is Nothing) OrElse (context.Instance Is Nothing) OrElse (provider Is Nothing) Then
  74.                 Return value
  75.             End If
  76.             ' get the Editor Service object, exit if not there.
  77.             wfes = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
  78.             If (wfes Is Nothing) Then
  79.                 Return value
  80.             End If
  81.  
  82.             ' create the TrackBar control, set its properties
  83.             tb = New TrackBar()
  84.             tb.Size = New Size(50, 150)
  85.             tb.TickStyle = TickStyle.TopLeft
  86.             tb.TickFrequency = 45
  87.             tb.SetRange(0, 360)
  88.             tb.Orientation = Orientation.Vertical
  89.             ' initalize Value
  90.             tb.Value = CInt(value)
  91.  
  92.             ' show the control (this call won't call until the dropdown area is closed
  93.             wfes.DropDownControl(tb)
  94.  
  95.             ' the return value must be of the correct type
  96.             EditValue = CSng(tb.Value)
  97.             ' destroy the trackbar control
  98.             tb.Dispose()
  99.             tb = Nothing
  100.         End Function
  101.  
  102.         ' Close the dropdown area when the mouse button is released
  103.         Private Sub TB_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles tb.MouseUp
  104.             If Not (wfes Is Nothing) Then
  105.                 wfes.CloseDropDown()
  106.             End If
  107.         End Sub
  108.  
  109.         ' Let the property editor know that we want to paint the value.
  110.         Public Overloads Overrides Function GetPaintValueSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
  111.             ' In this demo we return True regardless of the actual editor
  112.             Return True
  113.         End Function
  114.  
  115.         ' Display a yellow circle to the left of the property value in the Properties window
  116.         ' with a black line forming the same angle as the value of the RotateAngle property.
  117.         Public Overloads Overrides Sub PaintValue(ByVal e As System.Drawing.Design.PaintValueEventArgs)
  118.             ' Get the angle in radians.
  119.             Dim a As Single = CSng(e.Value) * CSng(Math.PI) / 180.0!
  120.             ' Get the rectangle in which we can draw.
  121.             Dim rect As Rectangle = e.Bounds
  122.             ' Evaluate the radius of the circle
  123.             Dim r As Single = Math.Min(rect.Width, rect.Height) / 2.0!
  124.             ' Get the center point.
  125.             Dim p1 As New PointF(rect.Width / 2.0!, rect.Height / 2.0!)
  126.             ' Calculate where the line should end.
  127.             Dim p2 As New PointF(CSng(p1.X + Math.Cos(a) * r), CSng(p1.Y + Math.Sin(a) * r))
  128.             ' Draw the yellow filled circle.
  129.             e.Graphics.FillEllipse(Brushes.Yellow, rect.Width / 2.0! - r, rect.Height / 2.0! - r, r * 2, r * 2)
  130.             ' Draw the line.
  131.             e.Graphics.DrawLine(Pens.Black, p1, p2)
  132.         End Sub
  133.  
  134.     End Class
  135.  
  136. End Class
  137.